/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import model.config.MySQLQuery;
import model.pojo.*;
import model.util.MySQLProviders;
/**
 *
 * @author Admin
 */
public class CTDonDatHangDAO {
    
    public static int getSoNguoiMua(int MaPhieu)
    {
        int s=0;
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = "Select Count(*) as SoLuong From CTDonDatHang Where MaPhieu="+MaPhieu;
            ResultSet rs = helper.executeQuery(sql);
            while(rs.next())
            {
                s=rs.getInt("SoLuong");
            }        
        }
        catch(SQLException ex)
        {
            Logger.getLogger(NhaCungCapPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return s;
    }
    
    public static ArrayList<CTDHPOJO> getDanhSach(int MaDonDatHang)
    {
        ArrayList<CTDHPOJO> ct = new ArrayList<CTDHPOJO>();
        String sql = "Select * From CTDonDatHang where MaDonDatHang="+MaDonDatHang;
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while(rs.next())
            {
                CTDHPOJO temp = new CTDHPOJO();
                temp.setMaCTDonDatHang(rs.getInt("MaCTDonDatHang"));
                temp.setMaPhieu(rs.getInt("MaPhieu"));
                temp.setSoLuong(rs.getInt("SoLuong"));
                temp.setGiaBan(rs.getDouble("GiaBan"));
                temp.setThanhTien(rs.getDouble("ThanhTien"));
                temp.setSoThuTu(rs.getInt("SoThuTu"));
                temp.setMaDonDatHang(MaDonDatHang);
                
                ct.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CTDonDatHangDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return ct;
    }
    
    public static void ThemCTHD(CTDHPOJO cthd, MySQLProviders helper) throws SQLException
    {
       
        String sql = String.format("Insert into CTDonDatHang (`MaCTDonDatHang`,`MaDonDatHang`,`MaPhieu`,`SoLuong`,`GiaBan`,`ThanhTien`,`SoThuTu`) "
                + "values (NULL,'%d','%d','%d','%e','%e','%d')",
        cthd.getMaDonDatHang(),cthd.getMaPhieu(),cthd.getSoLuong(),cthd.getGiaBan(),cthd.getThanhTien(),cthd.getSoThuTu()); 
        int n = helper.executeUpdate(sql);
        if(n == 1)
        {
            System.out.println("Them chi tiet hoa don thanh cong");
            PhieuPOJO s1 = new PhieuPOJO();
            try {
                s1 = PhieuDAO.GetPhieu(cthd.getMaPhieu());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(CTDonDatHangDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(CTDonDatHangDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            s1.setSoLuongMua(s1.getSoLuongMua() + cthd.getSoLuong());
            PhieuDAO.CapNhatSoLuongMua(s1,cthd.getMaPhieu(),helper);
        }       
        else
            System.out.println("Them chi tiet hoa don that bai");
    }
    
    public static ArrayList<CTDHPOJO> getDanhSachCoPhanTrang(int trang) throws FileNotFoundException, IOException {
      ArrayList<CTDHPOJO> ds = new ArrayList<CTDHPOJO>(); 
      int soSanPhamTrenTrang = ThamSoDAO.laySoLuongTrenTrang();
      int batDau = (trang - 1) * soSanPhamTrenTrang;
      if(soSanPhamTrenTrang==batDau)
          batDau=1;     
      //mo ket noi
      MySQLProviders helper = new MySQLProviders();
      helper.open();
      
      //doc query tu file text
      //lay cau truy van tu file text
     MySQLQuery mySQLQuery=new MySQLQuery();
     mySQLQuery.Read("PHANTRANG");
     String sql=String.format(mySQLQuery.GetQuery(MySQLQuery.QueryKey.SELECT_ALL_CTDONDATHANG_PHANTRANG),batDau,soSanPhamTrenTrang);
      ResultSet rs = helper.executeQuery(sql);
      try {
         while (rs.next()) {
            CTDHPOJO temp = new CTDHPOJO();
            temp.setMaCTDonDatHang(rs.getInt("MaCTDonDatHang"));
            temp.setMaPhieu(rs.getInt("MaPhieu"));
            temp.setSoLuong(rs.getInt("SoLuong"));
            temp.setGiaBan(rs.getDouble("GiaBan"));
            temp.setSoThuTu(rs.getInt("SoThuTu"));  
            temp.setMaDonDatHang(rs.getInt("MaDonDatHang"));
            ds.add(temp);
         }
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      helper.close();
      return ds;
   }
    
     public static CTDHPOJO getById(int id){
       CTDHPOJO obj=new CTDHPOJO();
       MySQLProviders helper = new MySQLProviders();
       helper.open();
       ResultSet rs = helper.executeQuery("select *from ctdondathang where mactdondathang="+id);
       try {
           while (rs.next()) {            
            obj.setMaCTDonDatHang(rs.getInt("MaCTDonDatHang"));
            obj.setMaPhieu(rs.getInt("MaPhieu"));
            obj.setSoLuong(rs.getInt("SoLuong"));
            obj.setGiaBan(rs.getDouble("GiaBan"));
            obj.setSoThuTu(rs.getInt("SoThuTu"));  
            obj.setMaDonDatHang(rs.getInt("MaDonDatHang"));
            
         }
       }catch(Exception e){
           
       }finally{
           helper.close();
       }
       return obj;
   }
}
